<?php

namespace App\Http\Middleware;

use Closure;

class OperationLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (setting('operator_log') == 1){
            $user_id = 0;
            $user_name = '';
            if(auth('admin')->check()) {
                $auth = auth('admin')->user();
                $user_id = $auth->id;
                $user_name = $auth->name;
            }
            $input = $request->all();
            $log = new \App\Models\OperationLog();
            $log->setAttribute('user_id', $user_id);
            $log->setAttribute('user_name', $user_name);
            $log->setAttribute('path', $request->path());
            $log->setAttribute('method', $request->method());
            $log->setAttribute('ip', $request->ip());
            $log->setAttribute('input', json_encode($input, JSON_UNESCAPED_UNICODE));
            $log->save();
        }
        return $next($request);
    }
}
